package com.example.uibestpractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    EditText editMessage;
    Button send;
    ArrayList<Message> messages = new ArrayList<>();
    MessageRecyclerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUi();
        initRecyclerView();
        setFunction();
    }

    private void setFunction() {
        send.setOnClickListener(v -> {
            String msg = editMessage.getText().toString();
            Message message = new Message(msg,Message.SENT_MESSAGE);
            messages.add(message);
            editMessage.setText("");
            adapter.notifyItemChanged(messages.size());
        });
    }

    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(manager);
        adapter = new MessageRecyclerAdapter(messages);
        recyclerView.setAdapter(adapter);
    }

    private void initUi() {
        recyclerView = findViewById(R.id.recyclerView_Message);
        editMessage = findViewById(R.id.editText_Massage);
        send = findViewById(R.id.button_send);
    }
}